package com.xjx.controller;

import com.alibaba.fastjson.JSONObject;
import com.xjx.pojo.PageInfo;
import com.xjx.pojo.User;
import com.xjx.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.sql.Date;

@Controller
@RequestMapping("/user")
@SessionAttributes("user")
public class UserController {
    @Autowired
    private UserService userService;


    @PostMapping("/login")
    public String login(Model model,@RequestParam("userCode") String userCode,
                        @RequestParam("userPassword") String userPassword){
        User user = userService.login(userCode,userPassword);
        if (user==null){
            return "login";
        }else {
            model.addAttribute("user",user);
            return "change";
        }
    }
    @GetMapping("/findByPage")
    public ModelAndView findByPage(@RequestParam(defaultValue = "1")int currentPage,
                                   @RequestParam(defaultValue = "5")int size,
                                   @RequestParam(required = false) String userName,
                                   @RequestParam(required = false) String userCode,
                                   @RequestParam(required = false) String startDate,
                                   @RequestParam(required = false) String endDate){
        ModelAndView modelAndView = new ModelAndView();
        PageInfo<User> pageInfo = userService.findByPage(currentPage,size,userName,userCode,startDate,endDate);
        System.out.println(pageInfo);
        modelAndView.setViewName("index");
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.addObject("userName",userName);
        modelAndView.addObject("userCode",userCode);
        modelAndView.addObject("startDate",startDate);
        modelAndView.addObject("endDate",endDate);
        return modelAndView;
    }
    @ResponseBody
    @PostMapping("/save")
    public String save(String userCode,String userName,String  userPassword,Integer gender, Date birthday,
                     String address){
        User user = new User();
        user.setUserCode(userCode);
        user.setUserName(userName);
        user.setUserPassword(userPassword);
        user.setGender(gender);
        user.setBirthday(birthday);
        user.setAddress(address);
        JSONObject object = new JSONObject();
        if (userService.save(user)>0){
            object.put("code",200);
            object.put("message","success");
        }else {
            object.put("code",400);
            object.put("message","fail");
        }
        return object.toJSONString();
    }
    @GetMapping("/findById")
    public ModelAndView findById(Integer id){
        ModelAndView modelAndView = new ModelAndView();
        User user = userService.findById(id);
        modelAndView.addObject("user",user);
        modelAndView.setViewName("addUser");
        return modelAndView;
    }
    @ResponseBody
    @PostMapping("/modify")
    public String modify(Integer id,String userCode,String userName,String  userPassword,Integer gender,
                         Date birthday,
                       String address){
        User user = new User();
        user.setId(id);
        user.setUserCode(userCode);
        user.setUserName(userName);
        user.setUserPassword(userPassword);
        user.setGender(gender);
        user.setBirthday(birthday);
        user.setAddress(address);
        JSONObject object = new JSONObject();
        if (userService.modify(user)>0){
            object.put("code",200);
            object.put("message","success");
        }else {
            object.put("code",400);
            object.put("message","fail");
        }
        return object.toJSONString();
    }
    @ResponseBody
    @PostMapping("/delete")
    public String delete(Integer id){
        JSONObject object = new JSONObject();
        if(userService.delete(id) > 0){
            object.put("code",200);
            object.put("message","success");
        }else {
            object.put("code",400);
            object.put("message","fail");
        }
        return object.toJSONString();
    }
}
